import api from '../axios'


export default{
    // 规范化时间格式
    formateDate(time){
        if(!time)
            return '';
        let date = new Date(time);
        return date.getFullYear() + "-" + (date.getMonth()+1) + "-" + date.getDate() + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
    },
    // 简单的检查表单登录
    checkInfo(data){
       return new Promise(async (resolve) => {
            let res = await api.ajax({
                method: 'post',
                url: '/user/login',
                data: {
                    username: data.username,
                    password: data.password,
                }
            });
            console.log(res);

            if(res.code == 1){
                localStorage.setItem('token', res.data.token)
                return resolve('success')
                
            }
                
            return  resolve('faild')
       })
    },
    //通过审核
    memberVerify(data){
        return new Promise(async (resolve) => {
            let res = await api.ajax({
                method: 'post',
                url: '/members/verify',
                data: {
                    id: data.id
                }
            });
            if(res.code == 1){
                return resolve('success')
            }
            return  resolve('faild')
        })
    },
    // localstorage读取数据
    setStorage(name,data){
        let dataType = typeof data;

        if (dataType == 'object'){
            window.localStorage.setItem(name,JSON.stringify(data));
        }else if(['number','string','boolean'].indexOf(dataType)>=0){
            window.localStorage.setItem(name,data);
        }else{
            alert("该类型不能用于本地存储");
        }
    },

    // localstorage取数据
    getStorage(name){
        let data = window.localStorage.getItem(name)
        if(data){
            return JSON.parse(data);
        }else{
            return '';
        }
    },

    // localstorage移除数据
    removeStorage(name){
        window.localStorage.removeItem(name);
    }

}